import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv, ConfigEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path, { resolve } from "path";
import postCssPxToRem from "postcss-pxtorem";

export default defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd());
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },

    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${resolve(
              "src/theme/common.less"
            )}";`,
          },
          javascriptEnabled: true,
        },
      },
      // postcss: {
      //   plugins: [
      //     postCssPxToRem({
      //       rootValue: 37.5,
      //       propList: [
      //         "width",
      //         "height",
      //         "padding",
      //         "margin",
      //         "margin-top",
      //         "margin-left",
      //         "margin-right",
      //         "margin-bottom",
      //         "padding-top",
      //         "padding-left",
      //         "padding-right",
      //         "padding-bottom",
      //       ],
      //       selectorBlackList: ["ant", "no", "noo", "common-warp"],
      //       minPixelValue: 1,
      //       unitPrecision: 6,
      //       mediaQuery: false,
      //     }),
      //   ],
      // },
    },
    base: env.VITE_PUBLIC_PATH,
    server: {
      host: "0.0.0.0", //监听所有ip
      port: env.VITE_PORT as unknown as number,
      hmr: true,
      proxy: {
        "/api": {
          target: env.VITE_PROXY_URL,
          ws: true,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
